<?php
class Controller_Product extends FLEA_Controller_Action
{
	private $_ModelProduct;
	private $_ModelUserInfo;
	private $_ModelSearch;
	private $_ModelDeclareCompany;
	private $_ModelProductCompany;
	private $siteInfo;
	private $user;
	private $userInfo;
	
	function Controller_Product(){
		$this->siteInfo = FLEA::getAppInf('siteInfo');//get site information
		
		$rbac =& FLEA::getSingleton('FLEA_Rbac');	
		$this ->user = $rbac->getUser();//get login user's information

		$this ->_ModelUserInfo = & FLEA::getSingleton('Model_UserInfo');		
		$this->userInfo = $this ->_ModelUserInfo->analysisUserInfo($this ->_ModelUserInfo,$this ->user['ID']);//get more user's information
	}
	
	function actionIndex(){
		isset($_GET['page'])?$currentPage=$_GET['page']:$currentPage=0;
		
		$this->_ModelSearch = & FLEA::getSingleton('Model_Search');
		$conditions = "user_id = " . $this->user['ID'];
		$sort = 'createtime desc';
		
		FLEA::loadHelper('pager');
		$pager = new FLEA_Helper_Pager($this->_ModelSearch, $currentPage,20,$conditions,$sort);
		
		$viewData = array(
			'siteInfo' => $this->siteInfo,
			'userInfo' => $this->userInfo,
			'list' => $pager->findAll(),
			'pagerData' => $pager->getPagerData(),
		);

		$this->_executeView('ProductList.html', $viewData);
	}
	
	
	function actionIncrease(){
		$material = FLEA::getAppInf('material');
		$isForeign = FLEA::getAppInf('isForeign');
		$class = FLEA::getAppInf('class');
		$pack = FLEA::getAppInf('pack');
		$companyNature = FLEA::getAppInf('companyNature');
		
		$this->_ModelDeclareCompany = & FLEA::getSingleton('Model_DeclareCompany');
		$conditions = "user_id = " . $this->user['ID'];
		$company = $this->_ModelDeclareCompany->find($conditions);
		
		if(empty($company['declare_id'])){
			$msg = '申报企业信息尚未添加，请填好企业信息后再提交产品申请';
			js_alert($msg,'',url('Company','Info'));
		}
		
		$_GET['kind']?$kind = $_GET['kind']:$kind = 1;
		
		switch($kind){
			case 1:$classArray = $class[0];
			break;
			case 2:$classArray = $class[1];
			break;
			case 3:$classArray = $class[2];
			break;
		}
		
//		dump($classArray);exit;
		$num = count($classArray['directoryName']);

		$viewData = array(
			'user' => $this->user,
			'userInfo' => $this->userInfo,
			'siteInfo' => $this -> siteInfo,
			'user_id' => $this->user['ID'],
			'do' => 'increase',
			'material'=>$material,
			'isForeign'=>$isForeign,
			'classArray'=>$classArray,
			'class'=>$class,
			'pack'=>$pack,
			'companyNature'=>$companyNature,
			'findId'=>$kind,
			'num'=>$num,
		);
		
//		dump($viewData);exit;

		$this->_executeView('ProductIncreaseNerve.html', $viewData);
	}
	
	function actionEdit(){
			
		is_numeric($_GET['id'])?$id = $_GET['id']:redirect(url('Product'));
		$this ->_ModelProduct = & FLEA::getSingleton('Model_Product');
		$conditions = "id = " . $id . " and user_id = " . $this->user['ID'];
		$result = $this ->_ModelProduct -> find($conditions);
		
		if(empty($result)){
			$msg = '你尚未申报任何产品信息';
			js_alert($msg,'',url('Product','Increase'));
		}
		if(((time() - strtotime($result['createtime']))/86400) > $this ->siteInfo['appLimitDay']){
			$msg = '该产品已经过了自行修改期，如需修改请联系系统管理员';
			js_alert($msg,'',url('Product','Index'));
		}
		
		$material = FLEA::getAppInf('material');
		$isForeign = FLEA::getAppInf('isForeign');
		$class = FLEA::getAppInf('class');
		$pack = FLEA::getAppInf('pack');
		$companyNature = FLEA::getAppInf('companyNature');
		
		switch($result['findId']){
			case 1:$classArray = $class[0];
			break;
			case 2:$classArray = $class[1];
			break;
			case 3:$classArray = $class[2];
			break;
		}
		
		$num = count($classArray['directoryName']);
//		dump($result);exit;
		$classTmp = explode(':',$result['package']);
		$moreTmp = explode(',',$classTmp[1]);
		
		$result['package'] = array('class'=>$classTmp[0],'more'=>$moreTmp);	
//		dump($result['package']);exit;
		$viewData = array(
			'siteInfo' => $this->siteInfo,
			'userInfo' => $this->userInfo,
			'list' => $result,
			'do'=>'edit',
			'id' => $id,
			'material'=>$material,
			'isForeign'=>$isForeign,
			'classArray'=>$classArray,
			'class'=>$class,
			'pack'=>$pack,
			'companyNature'=>$companyNature,
			'findId'=>$result['findId'],
			'num'=>$num,
		);
		
//		dump($viewData);exit;
		$this->_executeView('ProductEditNerve.html', $viewData);
		
	}
	
	function actionShow(){
		is_numeric($_GET['id'])?$id = $_GET['id']:redirect(url('Product'));
		$this ->_ModelProduct = & FLEA::getSingleton('Model_Product');
		$conditions = "id = " . $id . " and user_id = " . $this->user['ID'];
		$result = $this ->_ModelProduct -> find($conditions);
//		dump($result);exit;
		if(is_array($result)){
			$_ModelProductSubitem = FLEA::getSingleton('Model_ProductSubitem');
			$conditions = 'productId = ' . $id;
			$list = $_ModelProductSubitem -> findAll($conditions);
		}else{
			redirect(url('Product'));
		}
		$class = FLEA::getAppInf('class');
		$_GET['kind']?$kind = $_GET['kind']:$kind = 1;
		
		switch($kind){
			case 1:$classArray = $class[0];
			break;
			case 2:$classArray = $class[1];
			break;
			case 3:$classArray = $class[2];
			break;
		}
//		dump($list);exit;
		foreach ($list as $key => $value){
			$tmp = explode('|',$value['package']);
			
			$classTmp = explode(':',$tmp[0]);
			$moreTmp = explode(',',$classTmp[1]);
			$list[$key]['package'] = array('class'=>$classTmp[0],'more'=>$moreTmp);	
			
			$selfTmp = explode(',',$tmp[1]);
			$list[$key]['package']['self'] = $selfTmp[0];
			$list[$key]['package']['other'] = $selfTmp[1];
			
			$idTmp = explode('_',$selfTmp[0]);
			$list[$key]['package']['id'] = $idTmp[0];
		}
//		dump($list);exit;
		$viewData = array(
			'list' => $list,
			'class'=> $class,
			'classArray'=>$classArray,
			'do'=>'edit',
		);
//		dump($viewData);exit;
		$this->_executeView('ShowNumber.html', $viewData);
		
	}
	
	function actionChange(){
//		dump($_POST);
//		exit;
		foreach($_POST as $_key=>$_value){
			!ereg("^\_[A-Z]+",$_key) && $$_key=$_POST[$_key];
		}
			
		$conditions = 'mark = "' . $this->user[ID] . '+' . $time . '+' . $rand . '"';
		$_ModelTemporary = FLEA::getSingleton('Model_Temporary');
		$arrangs = $_ModelTemporary -> findAll($conditions);
			
		$this ->_ModelProduct = & FLEA::getSingleton('Model_Product');
		
//		$package = $_POST['classArray'] . ":" . implode(",",$_POST['packages']);
		
//		dump($package);exit;
		$p_company_column = array('nature','name','permitNumber','permitAddress','permitFunds','permitRealfunds','permitValidDate','licenceNumber','licenceAddress','licenceArea','licenceValidDate');
		$cert_column = array('oldNumber','oldValidDate','noteNumber','noteValidDate','newNumber','newValidDate','FDAnumber','FDAvalidDate','ECnumber','ECvalidDate','CMDproductNumber','CMDprodactDate','CMDqualityNumber','CMDqualityDate','CMDproductISOnumber','CMDproductISOdate',); 
		
		$list = array(
			'name' => $name,
			'range' => $range,
			'part' => $part,
			'norm' => $norm,
			'pack'=> $pack,
			'sterilizing'=> $sterilizing,
			'position'=>$position,
			'positionMore'=>$positionMore,
			'directoryName'=>$directoryName,
			'clinicalName'=>$clinicalName,
			'material'=>$material,
			'materialMore'=>$materialMore,
			'brand'=>$brand,
			'location'=>$location,
			'isForeign'=>$origin,
			'findId'=>$findId,
			'product_c'=>$product_c,
			'user_id'=>$user_id,
			//'package'=>$package,			
		);
		
		if(isset($cert) || isset($cert_for)){
//			dump($_POST['oldNumber']);
			foreach ($cert as $c_key => $c_value){
				if(empty($c_value)&&empty($cert_for[$c_key]))continue;
				$name = $cert_column[$c_key];
				$list['cert'][$name] = $cert_for[$c_key]?addslashes($cert_for[$c_key]):$c_value; 
			}
		}
		
		if(isset($p_company)){
			foreach ($p_company as $p_key => $p_value){
				if(empty($p_value))continue;
				$name = $p_company_column[$p_key];
				$list['p_company'][$name] = $p_value; 
			}
		}
			
		if($do == 'increase'){
			$list['createtime'] = date('Y-m-d H:i:s');
			$msg = '产品申报成功，你可以在未来' . $this->siteInfo['appLimitDay'] . '天内修改申报内容';
		}elseif($do == 'edit'){
			$msg = '信息修改成功';
		}
		
//		dump($list);exit;
		$productId = $this ->_ModelProduct->create($list);
		$_ModelProductSubitem = FLEA::getSingleton('Model_ProductSubitem');
		
		foreach($arrangs as $key => $arrang){
			unset($arrangs[$key]['mark']);
			$arrangs[$key]['productId'] = $productId;
		}
//		dump($arrangs);
		$_ModelProductSubitem -> createRowset($arrangs);	
		js_alert($msg,'',url('Product','Index'));		
		
	}
	
	function actionAddMore(){
		foreach($_GET as $_key=>$_value){
			!ereg("^\_[A-Z]+",$_key) && $$_key=$_GET[$_key];
		}
		$class = FLEA::getAppInf('class');
		$_ModelTemporary = FLEA::getSingleton('Model_Temporary');
		
		$Modelrand = & FLEA::getSingleton('Model_Code');
		$randnew = $Modelrand ->getCode(5);
		$timenew = time();
		
		if($rand && $time){
			$conditions = 'mark = "' . $this->user[ID] . '+' . $time . '+' . $rand . '"';
			$field = 'mark';
			$value = $this->user[ID] . '+' . $timenew . '+' . $randnew;
			$_ModelTemporary->updateField($conditions, $field, $value);
		}
		
		$_GET['kind']?$kind = $_GET['kind']:$kind = 1;
		
		switch($kind){
			case 1:$classArray = $class[0];
			break;
			case 2:$classArray = $class[1];
			break;
			case 3:$classArray = $class[2];
			break;
		}
		$num = count($classArray['directoryName']);
		$viewData = array(
			'siteInfo' => $this->siteInfo, 
			'user_id' => $this->user['ID'],
			'time' => $timenew,
			'rand' => $randnew,
			'class'=> $class,
			'classArray'=>$classArray,
			'num'=>$num,
		);
//		dump($viewData);exit;
		$this->_executeView('AddNumber.html', $viewData);
	}
	
	function actionTemporary(){
//		dump($_POST);exit;
		foreach($_POST as $_key=>$_value){
			!ereg("^\_[A-Z]+",$_key) && $$_key=$_POST[$_key];
		}
//		if(empty($productNumber[0])&&empty()){
//			js_alert('请填写内容后提交','',url('Product','AddMore'));
//		}
//		dump($hospital3_name);exit;
		
		foreach($productNumber as $keys => $loop){
			if(empty($model[$keys])&&empty($format[$keys])){
				js_alert('请填写内容后提交','',url('Product','AddMore'));
			}
			$packSelect = 'packages'.($keys + 1);
//			dump($packSelect);exit;
			$package = $classArray[$keys] . ":" . implode(",",$$packSelect);
			if(empty($package)){
				js_alert('组套为必填','',url('Product','AddMore'));
			}
			if($self[$keys] && $other[$keys]){
				$package .= '|'. $self[$keys] . ',' . $other[$keys];
			}
			$list[]=array(
				'productNumber' => $productNumber[$keys],
				'format' => $format[$keys],
				'model' => $model[$keys],
				'outland1' => $outland1[$keys],
				'outland2' => $outland2[$keys],
				'outland3' => $outland3[$keys],
				'outland4' => $outland4[$keys],
				'hospital1_name' => $hospital1_name[$keys],
				'hospital1_price' => $hospital1_price[$keys],
				'hospital1_person' => $hospital1_person[$keys],
				'hospital1_telephone' => $hospital1_telephone[$keys],
				'hospital2_name' => $hospital1_name[$keys],
				'hospital2_price' => $hospital1_price[$keys],
				'hospital2_person' => $hospital1_person[$keys],
				'hospital2_telephone' => $hospital1_telephone[$keys],
				'hospital3_name' => $hospital1_name[$keys],
				'hospital3_price' => $hospital1_price[$keys],
				'hospital3_person' => $hospital1_person[$keys],
				'hospital3_telephone' => $hospital1_telephone[$keys],
				'mark' => $_POST['user_id'] . '+' .  $_POST['time'] . '+' . $_POST['rand'],
				'package'=>$package,
			);
		}
//		dump($list);exit;
		$_ModelTemporary = FLEA::getSingleton('Model_Temporary');
		$_ModelTemporary-> saveRowset($list);
		
		js_alert('添加成功','window.close()',url('Product','AddMore'));
	}
} 
?>